
<template>
<!-- 面包屑 -->
  <div>
    <el-breadcrumb :separator-icon="ArrowRight">
      <el-breadcrumb-item>首页</el-breadcrumb-item>
      <el-breadcrumb-item>日常管理</el-breadcrumb-item>
      <el-breadcrumb-item>会议管理</el-breadcrumb-item>
      <el-breadcrumb-item><b>会议室信息维护</b></el-breadcrumb-item>
    </el-breadcrumb>
  </div>
<!-- 搜索框 -->
  <div style="margin:40px 20px 20px 20px">
    <el-form :inline="true" ref="searchFormRef" :model="searchFormModel" class="demo-form-inline" style="margin-top: 20px">
      <el-form-item label="会议室名" prop="cname">
        <el-input v-model="searchFormModel.cname" placeholder="请输入会议室名称" clearable />
      </el-form-item>
      <el-form-item label="会议室编号" prop="cno">
        <el-input v-model="searchFormModel.cno" placeholder="请输入会议室编号" clearable />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="searchOnSubmit">
          <el-icon><Search /></el-icon>&nbsp;查询
        </el-button>
        <el-button type="success" @click="dialogAddState=true" >
          <el-icon><Plus /></el-icon>&nbsp;添加
        </el-button>
      </el-form-item>
    </el-form>
  </div>
<!--  表格-->
  <div style="margin:20px">
    <el-table :data="info" stripe border style="width: 100%">
      <el-table-column prop="id" label="序号" v-if="show"/>
      <el-table-column prop="cname" label="会议室名"/>
      <el-table-column prop="cno" label="会议编号"/>
      <el-table-column prop="caption" label="特殊设备说明"  />
      <el-table-column prop="position" label="位置"  />
      <el-table-column prop="contain" label="容纳人数" />
      <el-table-column prop="note" label="备注" />
      <el-table-column prop="status" label="当前状态" />
      <el-table-column label="操作" width="180">
        <template #default="scope">
          <el-button
              size="small"
              type="primary"
              @click="tableEditHandler(scope.$index, scope.row)">
            <el-icon><Edit /></el-icon>&nbsp;编辑
          </el-button>
          <el-button
              size="small"
              type="danger"
              @click="tableDeleteHandler(scope.$index, scope.row)">
            <el-icon><Delete /></el-icon>&nbsp;删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
  <!--  分页-->
  <div class="page">
    <el-pagination
        v-model:current-page="currentPage"
        v-model:page-size="pageSize"
        :page-sizes="[5,8]"
        :small="small"
        :disabled="disabled"
        :background="background"
        layout="sizes, prev, pager, next"
        :total="totals"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
    />
  </div>
  <!--  编辑-->
  <el-dialog v-model="dialogEditState" title="修改会议室信息" width="380">
    <div>
      <el-form ref="formEditRef" :model="formEdit"
               label-width="120px">
        <el-form-item prop="id" v-if="false">
          <el-input v-model="formEdit.id" ></el-input>
        </el-form-item>
        <el-form-item prop="caption" label="特殊设备说明">
          <el-input v-model="formEdit.caption"  type="textarea"></el-input>
        </el-form-item>
        <el-form-item prop="note" label="备注">
          <el-input v-model="formEdit.note" ></el-input>
        </el-form-item>
        <el-form-item prop="status" label="当前状态">
          <el-select v-model="formEdit.status">
            <el-option label="启用" value="启用"></el-option>
            <el-option label="停止" value="停止"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
    </div>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="dialogEditOnReset">取消</el-button>
        <el-button type="primary"  @click="dialogEditOnSubmit" >确认</el-button>
      </div>
    </template>
  </el-dialog>
<!--新增  -->
  <el-dialog v-model="dialogAddState" title="添加会议室" width="380">
    <div>
      <el-form ref="formAddRef"  :model="formAdd"
               :rules="formAddRules"
               label-width="120px">
        <el-form-item prop="cname" label="会议室名称">
          <el-input v-model="formAdd.cname" maxlength="5" show-word-limit></el-input>
        </el-form-item>
        <el-form-item prop="cno" label="会议室编号">
          <el-input v-model="formAdd.cno" maxlength="10" show-word-limit></el-input>
        </el-form-item>
        <el-form-item prop="caption" label="特殊设备说明">
          <el-input v-model="formAdd.caption" type="textarea" rows="3"
          maxlength="50" show-word-limit></el-input>
        </el-form-item>
        <el-form-item prop="position" label="位置">
          <el-input v-model="formAdd.position" ></el-input>
        </el-form-item>
        <el-form-item prop="contain" label="容纳人数">
          <el-input-number v-model="formAdd.contain"  min="10" max="30"></el-input-number>
        </el-form-item>
        <el-form-item prop="note" label="备注">
          <el-input v-model="formAdd.note" type="textarea" rows="3"
                    maxlength="50" show-word-limit></el-input>
        </el-form-item>
        <el-form-item prop="status" label="当前状态">
          <el-select v-model="formAdd.status">
            <el-option label="启用" value="启用"></el-option>
            <el-option label="停止" value="停止"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
    </div>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="dialogAddOnReset">取消</el-button>
        <el-button type="primary"  @click="dialogAddOnSubmit" >确认</el-button>
      </div>
    </template>
  </el-dialog>
</template>
<script setup>
import {ArrowRight, Delete, Edit,Search,TopRight,Plus} from '@element-plus/icons-vue'
import {onMounted, reactive, ref} from "vue";
import request from "../../../utils/daily";
import {ElMessage, ElMessageBox} from "element-plus";
onMounted(()=>{
  getInfo();
})
//分页
const currentPage = ref(1)
const pageSize = ref(5)
const small = ref(false)
const background = ref(false)
const disabled = ref(false)
let totals=ref(0);
const info=ref([]);
//搜索
const searchFormRef=ref();
const searchFormModel=reactive({
  cname:'',
  cno:'',
})
const  getInfo=()=>{
  const params = {
    pageNum: currentPage.value,
    pageSize: pageSize.value,
    // 初始时不包含其他参数
  };
  if (searchFormModel.cname && searchFormModel.cname.trim() !== ''){
    params.cname=searchFormModel.cname.trim();
  }
  if (searchFormModel.cno && searchFormModel.cno.trim() !== ''){
    params.cno=searchFormModel.cno.trim();
  }
  request({
    url:'/chamber/page',
    method:'get',
    params,
  }).then((req)=>{
    if (200==req.data.errorCode){
      info.value=req.data.data.records;
      totals.value=req.data.data.total;
    }
  })
}
const searchOnSubmit=()=>{
  getInfo();
}
const handleSizeChange = (val) => {
  pageSize.value=val;
  getInfo();
}
const handleCurrentChange = (val) => {
  currentPage.value=val;
  getInfo();
}
//编辑
const formEditRef = ref()
const formEditData = {
  id:'',
  caption:'',
  status:'',
  note:''
}
// 编辑表单绑定数据模型
const formEdit = reactive({...formEditData})
const tableEditHandler = (rowIndex , rowData) => {
  console.log("-------------")
  console.log(rowIndex)
  console.log(rowData)
  formEdit.id=rowData.id
  formEdit.caption = rowData.caption
  formEdit.note = rowData.note
  formEdit.status = rowData.status
  dialogEditState.value = true
}
const dialogEditState = ref(false)
const dialogEditOnSubmit = () => {
  request({
    url: '/chamber/update',
    method: 'put',
    data: JSON.stringify(formEdit),
    headers: {'content-type': "application/json;charset=UTF-8"}
  }).then(response => {
    if (response.data.errorCode == 200) {
      ElMessage({
        type: 'success',
        message: '修改成功！',
      });
      dialogEditState.value = false
      getInfo();
    } else {
      ElMessage({
        type: 'error',
        message: '修改失败！',
      });
    }
  })
}
const dialogEditOnReset = () => {
  Object.assign(formEdit , formEditData)
  dialogEditState.value = false
}
//删除一条
const tableDeleteHandler = (rowIndex , rowData) => {
  ElMessageBox.confirm(
      '确认删除 : ' +rowData.id,
      'Warning',
      {
        confirmButtonText: '删除',
        cancelButtonText: '取消',
        type: 'warning',
      }
  ).then(() => {//点击了删除按钮
    request({
      method:'delete',
      url:'/chamber/del/'+rowData.id
    }).then(req=>{
      if (200==req.data.errorCode){
        ElMessage({
          type: 'success',
          message: '删除成功',
        })
        getInfo();
      }
    })
  })
      .catch(() => {//点击了取消按钮
        ElMessage({
          type: 'info',
          message: '取消删除',
        })
      })
}
//添加
//表单校验
const formAddRules = {
  cname: [
    { required: true, message: '请输入会议室的名字', trigger: 'blur' },
    { pattern: /^[\u4e00-\u9fa5]+$/, message: '会议室名字只能为汉字', trigger: 'blur' }
  ],
  cno: [
    { required: true, message: '请输入会议号', trigger: 'blur' },
    { pattern: /^\d+$/, message: '会议号只能为数字', trigger: 'blur' }
  ],
  contain: [
    { required: true, message: '请输入最大容纳量', trigger: 'blur' },
    { pattern: /^\d+$/, message: '最大容纳量只能为数字', trigger: 'blur' }
  ],
  status: [
    { required: true, message: '请选择状态', trigger: 'blur' }
  ]
};
const dialogAddState = ref(false)
// 新增表单引用
const formAddRef = ref()
// 新增表单初始化数据
const formAddData = {
  cname:'',
  cno:'',
  caption:'',
  position:'',
  contain:'',
  note:'',
  status:'',
}
// 新增表单绑定数据模型
const formAdd = reactive({...formAddData})   // ...formAddData 解构formAddData
const dialogAddOnSubmit = () => {
  formAddRef.value.validate((valid)=>{
    if (valid){
      //表单校验通过，提交逻辑
      request({
        method:'post',
        url:'/chamber/add',
        data:formAdd,
      }).then(req=>{
        if (200 == req.data.errorCode){
          ElMessage({
            type:'success',
            message:req.data.msg
          })
          Object.assign(formAdd , formAddData)
          getInfo();
        }
      })
      dialogAddState.value=false;
    }
  })
}
const dialogAddOnReset = () => {
  // 清空表单数据
  Object.assign(formAdd , formAddData)
  // 关闭对话框
  dialogAddState.value = false
}
</script>
<style scoped>
.el-breadcrumb{
  font-size: 18px;
  margin: 20px;
}
.page{
  margin-left: 30%;
}
</style>